<?php
/**
 * components/com_bookstore/views/bookstore/tmpl/default.php
 * @todo move all html outside php for validation
 */

// no direct access
defined('_JEXEC') or die('Restricted access'); 
//@review weren't these loaded in the view? 
JHTML::_( 'behavior.mootools' );
JHTML::stylesheet('modal.css');
JHTML::script('modal.js');

global $mainframe;
$document =& JFactory::getDocument();
//@review how does this method do? is that a portable path?
$document->setMetaData( 'refresh', '1200;url=http://'.JPATH_SITE.DS.'index.php?Itemid=5' );
?>
<script type="text/javascript">
		window.addEvent('domready', function() {
			SqueezeBox.initialize({}); 
			$$('a.modal').each(function(el) {
				el.addEvent('click', function(e) {
					new Event(e).stop();
					SqueezeBox.fromElement(el);
				});
			});
		});
  </script>
<?php
//@todo determine caption usage; possibly bundle js code; give a portable path.
?>
  <script type="text/javascript" src="stable/media/system/js/caption.js"></script>
<?php
//@todo move css to a file/bundle and load file, if necessary
//@note this is js related css
?>  
  <style type="text/css">

#image{
	width: 32px;
	height: 32px;
}
.box {
	margin: 0px auto;
	width: 32px;
	height: 32px;
	/*background: #4f8096;*/
	/*#ffffff;*/
	/*border: 1px solid #7199ab;*/
}
#box {
	margin: 0pt auto;
	width: 32px;
	height: 32px;
	/*background: #4f8096;*/
	/*#ffffff;*/
	/*border: 1px solid #7199ab;*/
}
 
#start {
	text-align: center;
	margin: .5em 0;
}

</style>

<div class="contentpaneopen" >
<?php
//@todo give a portable path
$link = 'index.php?option=com_bookstore&view=detail&id[]=';
$base = (string)'0';
$terms = JRequest::getVar('terms','');

if(!isset($this->items->result_set[$base]->title) && !empty($terms) ){
		?>
        <div style="width: 200px">
        <?php
        //@review all helper file need review
        require_once(JPATH_COMPONENT.DS.'helpers'.DS.'no_results.php'); 
	}elseif(!isset($this->items->result_set[$base]->title) && empty($terms)){
		//default value, provided if blank search or first fresh visit on page
		echo '<div style="width: 600px">';
		//@review all helper file need review
		include(JPATH_COMPONENT.DS.'helpers'.DS.'instructions.php');
	}else{
	$counter = '1';
	$terms = JRequest::getVar('terms');
	if($terms['offset'] == '1'){
		//erase terms[simple]
		//@todo this should be done with js code of client-side
		$terms['simple'] = '-- Quick Search --';
	}else{
		foreach($terms as $key=>$value){
			if( ($key != 'offset') && ($key != 'simple') ){
				$terms[$key] = '';
			}//end if 'offset or simple'
		}//end foreach
		$simple_search_helper = '<p class="simple_search_helper">Did find what you were looking for?  Try the Advanced Query search or ask a Bookseller!</p>';
	}//edn if ofset is 1
	JRequest::setVar('terms',$terms);
	?>
	
<?php
/**
 *  launch_map
 * 
 * @review is that actually used? marking as depreciated 08/23/08 -dn
 * @depreciated 	
 */
?>
	<script type="text/javascript">
//////////// map popup
function launch_map($str){
	var subDoc = window.open("/scripts/ajax/map_point_plot.php?"+$str,"Bookstore Map", "height=600, width=600");
}
	</script>
	<div style="clear:both; margin: 0px 0px 0px 5px; font-size:small;">
		<?php echo $this->pageNav; ?>
	</div>
	<?php
	echo '<div id="results" style="clear:both; margin-top: 10px">';
	foreach($this->items->result_set as $item){
		echo '<div class="result" id="item'.$item->isbn.'" style="float: left;" >';
		echo '<div id="box'.$item->isbn.'" style="float: left; width:0px; margin-bottom: 5px" ></div>';
?>
<div style="float: left; margin: 5px 5px 5px 5px;">
	<h1 style="margin: 5px 0px 0px 0px;"><?php echo $item->title; ?></h1>
	<div style="margin: -1px 0px 0px 2px">
	<span style="margin: -1px 0px 0px 0px;">Author:&nbsp;<?php echo $item->author;?></span><br />
	<span style="margin: 0px 0px 0px 0px;">ISBN:&nbsp;<?php echo $item->isbn;?></span>&nbsp;&nbsp;&nbsp;&nbsp;
		<span style="margin: 0px 0px 0px 0px;">Published:&nbsp;
			<?php
			/**
			 * @todo move this logic to model, the data date should always be a
			 *  std format (say epoch) and reformatted here for presentation purposes
			 */ 
			if(!empty($item->pub_date) && preg_match('/[0-9][0-9][0-9][0-9]-/', $item->pub_date) ){
				echo strftime('%m/%Y',strtotime($item->pub_date));
			}else{
				echo $item->pub_date;
			}
			?>&nbsp;&nbsp;&nbsp;
		</span><br />
	<span style="margin: 0px 0px 0px 0px;">
		<?php
			/**
				 * total_avail
				 * 
				 * show hyperlink section and quantity with nice disclaimer
				 * 
				 * @review is there someway to get this logic out of the template?
				 * @todo use portable path
				 * @review use config reference for modal window settings; more flexiable for maps, etc?
				 */
			if(!empty($item->sq1->instore->total_avail)){				
				?>
				<span id="avail<?php echo $item->isbn;?>" style="margin: 0px 0px 0px 0px;"><span style="color:green">Available in store:
				<?php echo " ".$item->sq1->instore->total_avail;?></span>&nbsp;&nbsp;&nbsp;Price: <?php echo $item->sq1->details[0]->retail_price_decimal; ?>
				</span><br />
				<span id="sect<?php echo $item->sq1->details[0]->dept_cat_number;?>" style="margin: 0px 0px 0px 0px;">Section:
				<?php /*<a href="#" onclick="launch_map('<?php echo 'sectionId='.$item->sq1->details[0]->dept_cat_number; ?>')"><?php (empty($item->sq1->details[0]->section_alias))? $var = $item->sq1->details[0]->dept_cat_desc : $var = $item->sq1->details[0]->section_alias ; echo " ".$var;?></a>
				*/
				(empty($item->sq1->details[0]->section_alias))? $text = $item->sq1->details[0]->dept_cat_desc : $text = $item->sq1->details[0]->section_alias ;
				$url	= '/scripts/ajax/map_point_plot.php?sectionId='.$item->sq1->details[0]->dept_cat_number;
				$status = "{handler: 'iframe', size: {x: 650, y: 650}}";
				$attribs['class']	= JText::_( 'modal' );
				$attribs['rel'] = "".$status."";
				$link = JHTML::_('link',$url, $text, $attribs);
				echo $link;
				?>
				</span>
				<?php			
			}else{
				//show hyperlink section and quantity with NOT IN STOCK DISCLAIMER
				?>
				<span id="avail<?php echo $item->isbn;?>" style="margin: 0px 0px 0px 0px;">Currently out of stock.
				<?php 
				echo '<span style="color:red">&nbsp;&nbsp;Ask a Bookseller about Availability and Special Order details</span>';
				?>
				</span>
				<?php
			}
		?>
	</span>
	</div>

 <?php 
 /////////////////////////////////////////////////////////////////////////
		//insert review icon if title has a review
		//@TODO use portable path; reference a theme/template icon
		if(count($item->sq1->reviews) > 0 ){
		//at least one review exists
		?>
		<div id="rev<?php echo $item->isbn;?>" style="width:200px;margin: 2px 0px 0px 0px;color:#1C0B7C; font-size:14px; font-weight:bold ">
			<img src="<?php echo "/stable/".'/images/star.png';?>" alt="BookPeople review icon" style="text-decoration:none; border: none; width:15px;height:15px" /> BookPeople Reviewed!
			<div style="display:none; background-color:#ffffff; color:#1C0B7C" id="rev_div<?php echo $item->isbn;?>">
			<?php 
				foreach($item->sq1->reviews as $review){
					echo '<div style="background-color:#E5EDEC; color:#000000; margin: 10px 0px 0px 0px; border-style: groove;border-width: 1px; border-color:#999999 ">';
					echo '<h2 style="float:left; color:#1C0B7C; font-weight:bold; text-decoration:underline; margin: 5px 0px 0px 5px;">'.ucfirst($review->cat_title).'</h2>';					echo '<div style="float:right;font-style:normal; font-size:9px; margin:5px 5px 0px 0px">&nbsp;&nbsp;&nbsp;review&nbsp;by&nbsp;'.$review->author.'</div>';
					echo '<h3 style="clear:both; margin: 0px 0px 2px 7px; font-style: italic">'.$review->title.'</h3><p style="margin: 0px 5px 5px 15px;">'.$review->introdescription.'</p>';
					echo '</div>';
				}//end foreach
				echo '<br />';
			?>
			</div>
		</div>
		<?php
		}//end if
		
		//insert show detail link (linked to js/mootool function/event)
 		?>
</div>
 <div style="clear:both; margin: 0px 0px 0px 0px">
	<div id="ann<?php echo $item->isbn;?>" style="width:100px;height:20px; margin: 15px 0px 25px 2px; ">
<?php
///////////////annotation mootools json////////////////
?>
		&nbsp;&nbsp;<a id="<?php echo "ann".$counter;?>" class="ann" href="#" name="<?php echo $item->isbn;?>">Show Details</a>
	</div>
	<hr class="item_break" />
 </div>
</div>
<?php
	$counter++;	
}//end foreach

?>
<div style="clear:both; margin: 10px 0px 10px 5px; font-size:small;">
	<?php echo $this->pageNav; ?>
    <?php echo $simple_search_helper; ?>
</div>
<?php
}//end if there are ANY results 

?>
</div>
</div>
